package com.huashi.dealer.modular.business.region.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huashi.dealer.commonEntity.Region;
import com.huashi.dealer.modular.business.region.mapper.RegionMapper;
import com.huashi.dealer.modular.business.region.param.RegionListItemVo;
import com.huashi.dealer.modular.business.region.param.RegionTreeVo;
import com.huashi.dealer.modular.business.region.service.RegionService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 地域Service实现类
 *
 * @author 系统生成
 * @since 2025-01-27
 */
@Service
public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> implements RegionService {

    @Resource
    private RegionMapper regionMapper;

    @Override
    public List<RegionListItemVo> getRegionListByParentId(Long parentId) {
        return regionMapper.selectByParentId(parentId);
    }

    @Override
    public String getRegionNameById(Long regionId) {
        if (regionId == null || regionId <= 0) {
            return "";
        }
        return regionMapper.selectNameById(regionId);
    }

    @Override
    public Long getRegionIdByNameAndParent(String regionName, Long parentId) {
        if (regionName == null || regionName.trim().isEmpty()) {
            return null;
        }
        return regionMapper.selectIdByNameAndParent(regionName.trim(), parentId);
    }

    @Override
    public List<RegionListItemVo> getProvinces() {
        return regionMapper.selectProvinces();
    }

    @Override
    public List<RegionListItemVo> getCitiesByProvince(Long provinceId) {
        if (provinceId == null || provinceId <= 0) {
            return List.of();
        }
        return regionMapper.selectCitiesByProvince(provinceId);
    }

    @Override
    public List<RegionListItemVo> getRegionsByCity(Long cityId) {
        if (cityId == null || cityId <= 0) {
            return List.of();
        }
        return regionMapper.selectRegionsByCity(cityId);
    }

    @Override
    public String getFullRegionName(Long provinceId, Long cityId, Long regionId) {
        StringBuilder fullName = new StringBuilder();
        
        // 添加省份名称
        if (provinceId != null && provinceId > 0) {
            String provinceName = getRegionNameById(provinceId);
            if (provinceName != null && !provinceName.isEmpty()) {
                fullName.append(provinceName);
            }
        }
        
        // 添加城市名称
        if (cityId != null && cityId > 0) {
            String cityName = getRegionNameById(cityId);
            if (cityName != null && !cityName.isEmpty()) {
                if (fullName.length() > 0) {
                    fullName.append(" ");
                }
                fullName.append(cityName);
            }
        }
        
        // 添加区县名称
        if (regionId != null && regionId > 0) {
            String regionName = getRegionNameById(regionId);
            if (regionName != null && !regionName.isEmpty()) {
                if (fullName.length() > 0) {
                    fullName.append(" ");
                }
                fullName.append(regionName);
            }
        }
        
        return fullName.toString();
    }

    @Override
    public List<RegionTreeVo> getRegionTree() {
        List<RegionTreeVo> result = new ArrayList<>();

        // 获取所有省份
        List<RegionListItemVo> provinces = getProvinces();
        if (CollectionUtils.isEmpty(provinces)) {
            return result;
        }

        for (RegionListItemVo province : provinces) {
            RegionTreeVo provinceTree = convertToTreeVo(province);
            result.add(provinceTree);

            // 获取该省份下的所有城市
            List<RegionListItemVo> cities = getCitiesByProvince(province.getRegionId());
            if (CollectionUtils.isEmpty(cities)) {
                continue;
            }

            List<RegionTreeVo> cityTrees = new ArrayList<>();
            for (RegionListItemVo city : cities) {
                RegionTreeVo cityTree = convertToTreeVo(city);
                cityTrees.add(cityTree);

                // 获取该城市下的所有区县
                List<RegionListItemVo> regions = getRegionsByCity(city.getRegionId());
                if (!CollectionUtils.isEmpty(regions)) {
                    List<RegionTreeVo> regionTrees = regions.stream()
                            .map(this::convertToTreeVo)
                            .toList();
                    cityTree.setChildren(regionTrees);
                }
            }
            provinceTree.setChildren(cityTrees);
        }

        return result;
    }

    /**
     * 将RegionListItemVo转换为RegionTreeVo
     */
    private RegionTreeVo convertToTreeVo(RegionListItemVo item) {
        RegionTreeVo treeVo = new RegionTreeVo();
        treeVo.setRegionId(item.getRegionId());
        treeVo.setRegionName(item.getRegionName());
        treeVo.setParentId(item.getParentId());
        treeVo.setLevel(item.getLevel());
        treeVo.setSort(item.getSort());
        return treeVo;
    }
}
